//
//  SceneCheckEffect.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/11/17.
//

import Foundation
import UIKit

enum SCheckEffectEnum: String {
    case text = "3"  // 文本
    case img = "4"   // 图片
    case date = "5"  // 时间
}

class SceneCheckEffect: UIControl {
    
    var checkR: CheckEffect!
    
    var title: String = "" {
        didSet {
            checkR.title = title
        }
    }

    var remark: String? {
        set {
            if type == .img || type == .text {
                remarkTV.text = newValue
            } else {
                dateTF.value = newValue
            }
        }
        get {
            if type == .img || type == .text {
                return remarkTV.text
            } else {
                return dateTF.value
            }
        }
    }
    
    var type: SCheckEffectEnum? {
        didSet {
            if type == .date {
                addDateControl()
            }
        }
    }
    
    var uploadType: UploadType? {
        didSet {
            if uploadType != nil {
                imgPicker.uploadType = uploadType!
            }
        }
    }

    var didSelectCheckWith:((_ isCheck: Bool?, _ imgs: [UIImage]?, _ remark: String?) -> ())?

    init(withEnable enable: Bool = true) {
        super.init(frame: .zero)
        isEnabled = enable
        
        self.checkR = CheckEffect(withEnable: enable)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        checkR.delegate = self
        addSubview(checkR)
        checkR.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp.top)
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(ScreenWidth - 20)
            make.bottom.equalTo(self.snp.bottom)
        }
    }
    
    func addTextView() {
        checkR.snp.remakeConstraints { (make) -> Void in
            make.top.equalTo(self.snp.top)
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(ScreenWidth - 20)
        }
        
        if remarkTV.superview == nil {
            addSubview(remarkTV)
            remarkTV.snp.makeConstraints { make in
                make.top.equalTo(checkR.isCheck == false ? imgPicker.snp.bottom : checkR.snp.bottom).offset(10)
                make.left.equalTo(self.snp.left).offset(13)
                make.right.equalTo(self.snp.right).offset(-13)
                make.height.equalTo(50)
                make.bottom.equalTo(self.snp.bottom)
            }
        }
    }
    
    func removeTextView() {
        if remarkTV.superview != nil {
            remarkTV.removeFromSuperview()
        }
        checkR.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp.top)
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(ScreenWidth - 20)
            make.bottom.equalTo(self.snp.bottom)
        }
    }
    
    func addDateControl() {
        checkR.snp.remakeConstraints { (make) -> Void in
            make.top.equalTo(self.snp.top)
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(ScreenWidth - 20)
        }
        
        if dateTF.superview == nil {
            addSubview(dateTF)
            dateTF.snp.makeConstraints { make in
                make.top.equalTo(checkR.snp.bottom).offset(10)
                make.left.equalTo(self.snp.left).offset(13)
                make.right.equalTo(self.snp.right).offset(-13)
                make.height.equalTo(56)
                make.bottom.equalTo(self.snp.bottom)
            }
        }
    }
    
    func addImagePicker() {
        checkR.snp.remakeConstraints { (make) -> Void in
            make.top.equalTo(self.snp.top)
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(ScreenWidth - 20)
        }
        
        if imgPicker.superview == nil {
            addSubview(imgPicker)
            imgPicker.snp.makeConstraints { make in
                make.top.equalTo(checkR.snp.bottom).offset(10)
                make.left.equalTo(self.snp.left)
                make.right.equalTo(self.snp.right)
                if checkR.isCheck == true {
                    make.bottom.equalTo(self.snp.bottom)
                }
            }
        }
    }
    
    func removeImagePicker() {
        if imgPicker.superview != nil {
            imgPicker.removeFromSuperview()
        }
        
        checkR.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp.top)
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(ScreenWidth - 20)
            make.bottom.equalTo(self.snp.bottom)
        }
    }
    
    lazy var imgPicker: BSImagePickerView = {
        let picker = BSImagePickerView()
        picker.didSelectImagesWith = {(imgs) -> () in
            self.didSelectCheckWith?(self.checkR.isCheck, imgs, self.remark)
        }
        return picker
    }()
    
    
    lazy var remarkTV: BSTextView = {
        let v = BSTextView()
        v.backgroundColor = .white
        v.placeholder = "请输入描述"
        v.layer.borderColor = UIColor.hex("#E0E0E0").cgColor
        v.layer.borderWidth = 0.5
        v.layer.cornerRadius = 4
        v.layer.masksToBounds = true
        return v
    }()
    
    lazy var dateTF: DateEffect = {
        let tf = DateEffect()
        tf.dateMode = .dateAndTime
        tf.placeholder = "时间"
        tf.maximumDate = Date()
        tf.didSelectDateWith = { (dateStr) -> () in
            self.didSelectCheckWith?(self.checkR.isCheck, self.imgPicker.images, self.remark)
        }
        return tf
    }()
}

extension SceneCheckEffect: CheckEffectDelegate {
    func valueChange(_ isCheck: Bool?) {
        switch type {
        case .text:
            if isCheck != nil && isCheck == false {
                addTextView()
            } else {
                removeTextView()
                remarkTV.text = ""
            }
            break
        case .img:
            if isCheck != nil {
                addImagePicker()
                if isCheck == false {
                    addTextView()
                }
            } else {
                removeImagePicker()
                removeTextView()
                imgPicker.images = []
                imgPicker.collectionView.reloadData()
                remarkTV.text = ""
            }
            break
        case .date: break
        default: break
        }
        
        didSelectCheckWith?(checkR.isCheck, imgPicker.images, self.remark)
    }
}
